﻿Imports DomusDotNet.Sbm.WS

Namespace DomusDotNet.Sbm.Wcf.Test
    <TestClass()>
    Public Class SharedBookshelfServiceTest

        Private _Client As Lazy(Of SharedBookshelfServiceClient) = New Lazy(Of SharedBookshelfServiceClient)

        Protected ReadOnly Property Client As SharedBookshelfServiceClient
            Get
                Return Me._Client.Value
            End Get
        End Property

        <TestMethod()>
        Public Sub Login_UserValid()
            Dim req = New LoginRequest With {.UserName = "Massimo",
                                            .Password = "Massimo"}


            Dim resp = Client.Login(req)

            Assert.IsInstanceOfType(resp, GetType(LoginResponse))
            Assert.IsNotNull(resp)
            Assert.AreEqual(True, resp.Result)
            Assert.AreEqual(0, resp.Errors.Count)
            Assert.AreNotEqual(Guid.Empty, resp.SessionGUID)
        End Sub

        <TestMethod()>
        Public Sub Login_UserUnknown()
            Dim req = New LoginRequest With {.UserName = "Massimo",
                                            .Password = "Bonanni"}

            Dim resp = Client.Login(req)

            Assert.IsInstanceOfType(resp, GetType(LoginResponse))
            Assert.IsNotNull(resp)
            Assert.AreEqual(False, resp.Result)
            Assert.IsTrue(resp.Errors.Count > 0)
            Assert.IsTrue(String.IsNullOrEmpty(resp.SessionGUID))
        End Sub

        <TestMethod()>
        Public Sub Login_UsernameEmpty()
            Dim req = New LoginRequest With {.UserName = "",
                                            .Password = "Bonanni"}

            Dim resp = Client.Login(req)

            Assert.IsInstanceOfType(resp, GetType(LoginResponse))
            Assert.IsNotNull(resp)
            Assert.AreEqual(False, resp.Result)
            Assert.IsTrue(resp.Errors.Count > 0)
            Assert.IsTrue(String.IsNullOrEmpty(resp.SessionGUID))
        End Sub

        <TestMethod()>
        Public Sub Login_PasswordEmpty()
            Dim req = New LoginRequest With {.UserName = "Massimo",
                                            .Password = ""}

            Dim resp = Client.Login(req)

            Assert.IsInstanceOfType(resp, GetType(LoginResponse))
            Assert.IsNotNull(resp)
            Assert.AreEqual(False, resp.Result)
            Assert.IsTrue(resp.Errors.Count > 0)
            Assert.IsTrue(String.IsNullOrEmpty(resp.SessionGUID))
        End Sub

        <TestMethod()>
        Public Sub Login_UsernameAndPasswordEmpty()
            Dim req = New LoginRequest With {.UserName = "",
                                            .Password = ""}

            Dim resp = Client.Login(req)

            Assert.IsInstanceOfType(resp, GetType(LoginResponse))
            Assert.IsNotNull(resp)
            Assert.AreEqual(False, resp.Result)
            Assert.IsTrue(resp.Errors.Count > 0)
            Assert.IsTrue(String.IsNullOrEmpty(resp.SessionGUID))
        End Sub

        <TestMethod()>
        Public Sub Logout_RightGuid()
            Dim req = New LogoutRequest With {.SessionGUID = Guid.NewGuid().ToString()}

            Dim resp = Client.Logout(req)

            Assert.IsInstanceOfType(resp, GetType(LogoutResponse))
            Assert.IsNotNull(resp)
            Assert.IsTrue(resp.Result)
            Assert.IsTrue(resp.Errors.Count = 0)
        End Sub

        <TestMethod()>
        Public Sub Logout_WrongGuid()
            Dim req = New LogoutRequest With {.SessionGUID = "sssss"}

            Dim resp = Client.Logout(req)

            Assert.IsInstanceOfType(resp, GetType(LogoutResponse))
            Assert.IsNotNull(resp)
            Assert.IsFalse(resp.Result)
            Assert.IsTrue(resp.Errors.Count > 0)
        End Sub

        <TestMethod()>
        Public Sub SearchBooks_WithoutFilters_RightGuid()
            Dim req = New SearchBooksRequest With {.SessionGUID = Guid.NewGuid.ToString()}

            Dim resp = Client.SearchBooks(req)

            Assert.IsInstanceOfType(resp, GetType(SearchBooksResponse))
            Assert.IsNotNull(resp)
            Assert.IsTrue(resp.Result)
            Assert.IsTrue(resp.Errors.Count = 0)
            Assert.IsNotNull(resp.Books)
            Assert.IsTrue(resp.Books.Count = 4)
        End Sub

        <TestMethod()>
        Public Sub SearchBooks_WithoutFilters_WrongGuid()
            Dim req = New SearchBooksRequest With {.SessionGUID = "dddd"}

            Dim resp = Client.SearchBooks(req)

            Assert.IsInstanceOfType(resp, GetType(SearchBooksResponse))
            Assert.IsNotNull(resp)
            Assert.IsFalse(resp.Result)
            Assert.IsTrue(resp.Errors.Count > 0)
            Assert.IsNull(resp.Books)
        End Sub

        <TestMethod()>
        Public Sub SearchBooks_AuthorFilters_RightGuid()
            Dim req = New SearchBooksRequest With {.SessionGUID = Guid.NewGuid.ToString(),
                                                   .AuthorFilter = "Erne"}

            Dim resp = Client.SearchBooks(req)

            Assert.IsInstanceOfType(resp, GetType(SearchBooksResponse))
            Assert.IsNotNull(resp)
            Assert.IsTrue(resp.Result)
            Assert.IsTrue(resp.Errors.Count = 0)
            Assert.IsNotNull(resp.Books)
            Assert.IsTrue(resp.Books.Count = 1)
        End Sub

        <TestMethod()>
        Public Sub BookReservation_RightId_RightGuid()
            Dim req = New BookReservationRequest With {.SessionGUID = Guid.NewGuid.ToString(),
                                                        .BookId = 1,
                                                        .ReservationName = "Massimo"}

            Dim resp = Client.BookReservation(req)

            Assert.IsInstanceOfType(resp, GetType(BookReservationResponse))
            Assert.IsNotNull(resp)
            Assert.IsTrue(resp.Result)
            Assert.IsTrue(resp.Errors.Count = 0)
            Assert.IsTrue(resp.ReservationResult)
            Assert.AreEqual(resp.BookId, req.BookId)
        End Sub
    End Class
End Namespace